import Storage from '../../utils/storage'
const STORAGE_SYSTEM_NAME = "systemRoutes"

const storage = new Storage()

const system = storage.getStorage(STORAGE_SYSTEM_NAME)
const routes = system? system.routes:null
// console.log(routes)

const state = {
  routes, // 系统路由，根据用户角色获取
}

const getters = {
  getHomeNavData (state) { // 获取所有路由信息
    return state.routes
  }
}

const mutations = {
  setHomeNavData (state, payload) { // 更改所有路由信息
    state.routes = payload
  },
  clearHomeNavData (state, payload) { // 清除路由信息
    state.routes = null
  }
}

const actions = {
  saveRoutes (context, commitThings) {
    return new Promise ((resolve, reject) => {
      storage.saveStorage(STORAGE_SYSTEM_NAME, { routes: commitThings })
      context.commit('setHomeNavData', commitThings)
      resolve()
    })
  },
  clearRoutes (context, commitThings) { 
    return new Promise ((resolve, reject) => {
    storage.removeStorage(STORAGE_SYSTEM_NAME)
    context.commit('clearHomeNavData', commitThings)
    resolve()
  })
  }
}

export default{
  namespaced: true,// 开启模块的命名空间
  state,
  getters,
  mutations,
  actions
}